package org.example.config;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import dev.langchain4j.agent.tool.ToolExecutionRequest;
import dev.langchain4j.data.message.AiMessage;

import java.io.IOException;

public class AiMessageSerializer extends JsonSerializer<AiMessage> {
    @Override
    public void serialize(AiMessage value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        gen.writeStartObject();

        if (value.text() != null) {
            gen.writeStringField("text", value.text());
        }

        if (value.toolExecutionRequests() != null) {
            gen.writeArrayFieldStart("toolExecutionRequests");
            for (ToolExecutionRequest request : value.toolExecutionRequests()) {
                // 这里假设 ToolExecutionRequest 可以直接序列化， 如果需要你可以进一步自定义序列化
                gen.writeObject(request);
            }
            gen.writeEndArray();
        }

        gen.writeEndObject();
    }
}
